package com.catsic.web.controllers;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.catsic.base.annotation.Log;
import com.catsic.base.controller.BaseController;
import com.catsic.base.utils.FtpUtils;
import com.catsic.base.utils.SpringContextUtil;
import com.catsic.core.organ.service.OrganService;
import com.catsic.mod.condition.AnnouncesCondition;
import com.catsic.mod.form.AnnouncesForm;
import com.catsic.mod.service.AnnouncesService;
import com.catsic.pub.entity.FileUpload;
import com.catsic.pub.file.service.FileUploadService;

/**
 * ClassName: AnnouncesController
 * Description: 消息Controller
 * author: zhoukai
 *
 */
@Controller
@RequestMapping("/announces")
public class AnnouncesController extends BaseController{
    
	@Autowired
	private AnnouncesService announcesService;
	
	@Autowired
	private OrganService organService;
	
	@Autowired
	private FileUploadService fileUploadService;
	
	@RequestMapping(value="/query",method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> query(AnnouncesCondition condition,Pageable pageable){
		Page<AnnouncesForm> page = announcesService.findAll(condition, pageable);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put(CONTENT, page);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	@RequestMapping(value="/create",method = RequestMethod.POST)
	@ResponseBody
	@Log(module = "通知公告管理", operation = "添加")
	public Map<String,Object> create(AnnouncesForm form,String []organIds){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put(CONTENT, announcesService.add(form,organIds));
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	@RequestMapping(value="/detail",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> findById(String id){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put(CONTENT, announcesService.findAnnouncesById(id));
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	@RequestMapping(value="/queryAnnouncesFile",method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> queryAnnouncesFile(AnnouncesCondition condition,Pageable pageable,HttpServletRequest request, HttpServletResponse response){
		Page<AnnouncesForm> page = announcesService.findAll(condition, pageable);
		List<AnnouncesForm> listAnnForm = new ArrayList<AnnouncesForm>();
		for(AnnouncesForm announcesForm : page){
				listAnnForm.add(announcesForm);
		}
		List<FileUpload> fileUploadList = new ArrayList<FileUpload>();
		for(AnnouncesForm announcesForm : listAnnForm){
			for(FileUpload fileUpload : fileUploadService.findByFid(announcesForm.getId())){
				if(fileUpload.getFileName().contains(".png") || fileUpload.getFileName().contains(".jpg")){
					fileUploadList.add(fileUpload);
				}
			}
		}
		
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("announces", listAnnForm);
		map.put(CONTENT, fileUploadList);
		map.put(RESULT, RESULT_SUCCESS);
		return map;
	}
	
	@RequestMapping(value="/queryImage",method = RequestMethod.GET)
	@ResponseBody
	public void queryImage(String id,HttpServletRequest request, HttpServletResponse response){
		FtpUtils ftp = new FtpUtils(SpringContextUtil.getMessage("ftpip"), Integer.valueOf(SpringContextUtil.getMessage("ftpport")).intValue(), SpringContextUtil.getMessage("ftpuser"), SpringContextUtil.getMessage("ftppassword"));
		ftp.login();
		FileUpload file = fileUploadService.findById(id);
		ServletOutputStream  output = null; 
		try{
			output = response.getOutputStream();
			ftp.getFtpClient().retrieveFile(file.getRandomFileName(), output);
			ftp.exit();
			output.flush();             
		}catch(IOException e){
			
		}finally{
			if(output!=null){
				try {
					output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}  
			}
		}
		response.setContentType("image/jpeg");  
		response.setHeader("Cathe-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
	}
	
	@RequestMapping(value="/queryPhoto",method = RequestMethod.GET)
	@ResponseBody
	public void queryPhoto(String filePath,HttpServletRequest request, HttpServletResponse response){
		BufferedImage image=null; 
		ServletOutputStream  output = null; 
		try { 
			image = ImageIO.read(new File(filePath)); 
		} catch (IOException e) { 
			System.out.println("读取图片文件出错！"+e.getMessage()); 
		} 
		double Ratio = 0.0; 
		//图片要缩放的比例            
		if ((image.getHeight() > 300) ||(image.getWidth() > 300)) {
			if (image.getHeight() > image.getWidth()){
				Ratio = 300.0 / image.getHeight();
			}else {                
				Ratio = 300.0 / image.getWidth();         
			} 
		}
		AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(Ratio, Ratio), null);        
		// 转换源 BufferedImage 并将结果存储在目标 BufferedImage 中。        
		image = op.filter(image,null); 
		try{
			output = response.getOutputStream();
			ImageIO.write((BufferedImage)image,"jpg",output); 
			output.flush();             
		}catch(IOException e){
			
		}finally{
			if(output!=null){
				try {
					output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}  
			}
		}
		response.setContentType("image/jpeg");  
		response.setHeader("Cathe-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
	}
	
	
	
//	@RequestMapping(value = "/delete")
//	@ResponseBody
//	public Map<String, Object> deleteById(String id){
//        Map<String,Object> map = new HashMap<String, Object>();
//        announcesService.delete(id);
//		map.put(RESULT, RESULT_SUCCESS);
//		return map;
//	}
}
